{% extends '@Admin/adminFramework/base/index.html.twig' %}
{% import '@Admin/adminFramework/loader/base.loader.twig' as loader_base %}
{% import '@Admin/adminFramework/loader/model.loader.twig' as loader_model %}
{% import '@Admin/adminFramework/loader/control.loader.twig' as loader_control %}
{% import '@Admin/adminFramework/config.html.twig' as config %}

{% block content %}
    {#之前#}
    {% block before %}{% endblock %}

    <div class="row-fluid" style="margin-bottom: 20px">
        {#统计#}
        {% block statistics %}
        {% endblock %}
    </div>

    {% block filtrate %}
        <div class="row-fluid">
            <div class="span12">
                <div class="widget">
                    <div class="widget-title">
                        <h4><i class="icon-search"></i>筛选</h4>
                        <span class="tools">
                        <a href="javascript:;" class="icon-chevron-down"></a>
                        <a href="javascript:;" class="icon-remove"></a>
                    </span>
                    </div>
                    <div class="widget-body">
                        {#
                            换行：在需要换行的元素后加这样一行代码   <div class="clearfix"></div>

                            前面留空或中间留空，在需要留空的下一个元素加class属性  layui-col-lg-offset3    后面的数字自己规定
                         #}
                        <form action="" method="get" class="form-horizontal" id="form">
                            {{ loader_control.label('行数：') | raw }}
                            {{ loader_control.text(app.request.get('rows', 15), '', 'number', 'input-mini', ' name = "rows" id="input-rows"') }}
                            <br>
                            {% block input %} {% endblock %}
                            <div class="form-actions" style="padding-bottom: 0">
                                {% block button %}
                                    {{ loader_control.button('<i class="icon-search"></i>筛选', 'button', 'btn blue', 'style = "margin-left:120px" id = "btn-search"')  | raw }}
                                    {{ loader_control.button('<i class="icon-remove"></i>重置', 'button', 'btn', 'style = "margin-left:20px" id = "btn-clear"') | raw }}
                                {% endblock %}
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    {% endblock %}
     <div class="row-fluid">
        <div class="widget">
            <div class="widget-title">
                <h4><i class="icon-reorder"></i>{% block title %}{% endblock %}</h4>
                <span class="tools">
                    <a href="javascript:;" class="icon-chevron-down"></a>
                    <a href="javascript:;" class="icon-remove"></a>
                </span>
            </div>
            <div class="widget-body">
                {#功能块#}
                {% block action %}
                {% endblock %}

                <form action="{{ ex_url | default }}" id="batchForm" style="overflow-x: auto;overflow-y: hidden">
                    <table class="table table-striped table-bordered dataTable" style="border-collapse:collapse">
                        <thead>
                            {% block th %} {% endblock %}
                        </thead>
                        <tbody>
                            {% block tbody %}
                                {% for item, value in info %}
                                     <tr>{% block td %}{% endblock %}</tr>
                                {% endfor %}
                            {% endblock %}
                        </tbody>
                    </table>
                </form>
                <div class="row-fluid">
                    <div class="span6">
                    </div>
                    <div class="span6">
                        <div class="dataTables_paginate paging_bootstrap pagination">
                            <div class="row-fluid">{{ paging.render | default('') | raw }}</div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
     </div>
{% endblock %}

{% block jquery %}
    {{ parent() }}
    <script src="{{ asset('bundles/admin/js/responseForm.js') }}?v=2017121"></script>
    <script>
    $(function () {

        var tableClass = $('.table.table-striped.table-bordered.dataTable');

        //列表中图片点击放大
        {% for key in info|default %}
            layer.photos({
                photos: '#layer-photos{{ loop.index0 }}',//同一个td里的img标签用一个id为 layer-photos{{ loop.index0 }} 的div套着
                shift: 5 //0-6的选择，指定弹出图片动画类型，默认随机
            });
        {% endfor %}

        //多选按钮
        var length = $('.checkbox-multipel').length;
        var num = 0;
        $('body').on('click','.checkbox-multipel',function () {
            if($(this).parent('span').hasClass('checked')){
                num++ ;
                if(num === length){
                    $('#checkboxAll').attr('checked',true).parent().addClass('checked')
                }
            }else{
                num--;
                if(num === length-1){
                    $('#checkboxAll').attr('checked',false).parent().removeClass('checked')
                }
            }
        });
        $('body').on('click','#checkboxAll',function () {
            if($(this).parent('span').hasClass('checked')){
                $('.checkbox-multipel').each(function () {
                    $(this).attr('checked',true).parent().addClass('checked')
                });
                num = length;
            }else{
                $('.checkbox-multipel').each(function () {
                    $(this).attr('checked',false).parent().removeClass('checked')
                });
                num = 0;
            }
        });

        //批量删除
        $('#batchDelete').click(function () {
            var data = $('#batchForm').serializeObject();
            if(num !== 0){
                layer.confirm('确认删除？','是否确认删除',function () {
                    $.ajax({
                        url: '{{ config.del_url()|trim }}',
                        type: 'POST',
                        data: data,
                        async:false,
                        dataType:'json',
                        success: function (res) {
                            if (res.errorCode == 0) {
                                layer.msg(res.message.message);
                                location.href = res.data.go_url;
                            } else {
                                layer.msg(res.message.message);
                            }
                        },
                        error:function () {
                            layer.msg('网络错误')
                        }
                    });
                })
            }else{
                layer.msg('请选择删除项目');
            }
        });

        //导入
        var uploadId = document.getElementById('import_id');
        var path_name = document.getElementById('import_path');

        if(uploadId){
            uploadId.addEventListener('change',function () {
                var fileValue = this.value;
                var index = fileValue.lastIndexOf('.');
                var suffix = fileValue.substring(index);

                if(suffix == '.xlsx' || suffix == '.xls' || suffix == '.xlsm' || suffix == '.xltx' || suffix == '.xltm' || suffix == '.xlsb' || suffix == '.xlam'){
                    var path = uploadFile().replace(/\\/g,'/');
                    var url = '{{ config.import_url()|trim }}';
                    path_name.value = path;
                    path = path_name.value;

                    $.post(url,{path:path},function (result) {
                        if (result.errorCode == 0) {
                            layer.msg(result.message.message);
                            location.href = result.data.go_url;
                        } else {
                            layer.msg(result.message.message);
                        }
                    })
                }else{
                    layer.msg('请上传Excel表格');
                    return false;
                }

            });

            function uploadFile() {
                var value = '';
                var data = new FormData();
                data.append('request', uploadId.files[0]);
                $.ajax({
                    url: '{{ path('service_resource_upload',{type:3}) }}',
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    data: data,
                    async:false,
                    cache: false,
                    dataType:'json',
                    success: function (result) {
                        if (result.errorCode == 0) {
                            layer.msg(result.message.message);
                            value = result.data.path_name ;
                        } else {
                            layer.msg(result.message.message);
                        }
                    },
                    complete:function () {
                        uploadId.value = '';
                    }
                });
                return value;
            }
        }

        //批量导出
        $('#batchExport').click(function () {
            if(num == 0){
                layer.msg('请选择导出项目');
            }else{
                $('#batchForm').submit();
            }
        });
        //固定table
        if($('thead th',tableClass).length < 7){
            tableClass.css('table-layout','auto');
        }else{
            tableClass.css('table-layout','fixed');
        }

        //提示工具  在需要隐藏的td上加属性  data-tooltip="你要显示的值";
        $('td[data-tooltip]').each(function () {
            $(this).addClass('a-line');

            var tooltip = $(this).attr('data-tooltip');
            var text = $(this).html();

            $(this).html('<div data-toggle="tooltip" class="a-line" data-placement="top" title="'+tooltip+'">'+text+'</div>');

        });
        $('[data-toggle="tooltip"]').tooltip();

        //分页溢出
        {#pageDataPrimary('{{ app.request.get('page') }}', {{ count | default(0) }}, '{{ app.request.get('rows') }}', {{ info | length }});#}

        //搜索按钮
        $('#btn-search').click(function () {
            var rows = $('#input-rows').val();
            var r = new RegExp('^[0-9]*[1-9][0-9]*$');

            if(!r.test(rows)){
                windowsMessage('行数请输入正整数');
                return;
            }

            if(rows > 1000){
                windowsMessage('行数不能大于1000');
                return;
            }

            {% block click_btn_search  %}
            {% endblock %}

            $('#form').submit();
        });

        //取消按钮
        $('#btn-clear').click(function () {
            {% block click_btn_clear  %}
                //表单重置
                $('#form')[0].reset();
            {% endblock %}
        });

    })
    </script>
{% endblock %}